﻿#region

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

#endregion

namespace Pob.GameObjects
{
    public class Paddle : GameObject
    {
        #region Fields

        private readonly Player _player;

        private Vector2 _movement;

        private double _seconds;

        private Settings _settings = SettingsManager.CurrentInstance.Settings;

        #endregion

        #region Constructors and Destructors

        public Paddle(Game game, string assetName, Vector2 position, float sizeFactor, Player player)
            : base(game, assetName, position, sizeFactor, Color.White)
        {
            _player = player;

            _movement = new Vector2(0, 300);
        }

        #endregion

        #region Public Properties

        public Player Owner
        {
            get
            {
                return _player;
            }
        }

        #endregion

        #region Public Methods and Operators

        public void SlowMovement(Ball ball)
        {
            if (ball.Speed < _movement.Y)
            {
                _movement = new Vector2(_movement.X, ball.Speed - 10);
            }

            _seconds = 0;
        }

        public override void Update(GameTime gameTime)
        {
            KeyboardState keyBoardState = Keyboard.GetState();

            if (_seconds < 1)
            {
                _seconds += gameTime.ElapsedGameTime.TotalSeconds;

                if (_seconds > 1)
                {
                    StartMovement();
                }
            }

            if (keyBoardState.IsKeyDown(_player.KeyUp))
            {
                Position -= (float)gameTime.ElapsedGameTime.TotalSeconds * _movement;

                if (Position.Y < (_settings.Height / 8f) + 15f)
                {
                    Position = new Vector2(Position.X, (_settings.Height / 8f) + 15f);
                }
            }

            if (keyBoardState.IsKeyDown(_player.KeyDown))
            {
                Position += (float)gameTime.ElapsedGameTime.TotalSeconds * _movement;

                if ((Position.Y + Textur.Height) > (_settings.Height - 15f))
                {
                    Position = new Vector2(Position.X, _settings.Height - 15f - Textur.Height);
                }
            }

            HitBox = new Rectangle((int)Position.X, (int)Position.Y, Textur.Width, Textur.Height);
        }

        #endregion

        #region Methods

        private void StartMovement()
        {
            _movement = new Vector2(0, 300);
        }

        #endregion
    }
}